package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyNanJingJuShengHong;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import static com.gitee.apanlh.web.http.HttpContentType.APPLICATION_JSON;

/**
 *  广州金贷普惠按揭服务公司
 *
 *  @author WJB
 */
@ApiPlan(productId = "310", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5JinDaiPuHui implements ApiOrgPhoneMd5ConvertHandler {

    private static final String BRAND = "35";

    private static final String SOURCE = "gzyf-axfq";
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(APPLICATION_JSON);
        return MapUtils.newHashMap(newMap -> {
            newMap.put("md5mobile", apiDataExchangeParameter.getPhoneMd5());
            newMap.put("brand", BRAND);
        });
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("data"), 1)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(APPLICATION_JSON);
        OrgApplyNanJingJuShengHong data = new OrgApplyNanJingJuShengHong();
        // 客户姓名
        data.setName(apiApplyParameter.getUserName());
        data.setMobile(apiApplyParameter.getPhone());
        // 城市(不包含市，比如“杭州市”应该是“杭州”)
        data.setCity(apiApplyParameter.getCity().replace("市", ""));
        // 是 否 有 车 (“ 有 ” 或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "无")
                    .when(apiApplyParameter.getCarInfo(), "2", "有")
                    .when(apiApplyParameter.getCarInfo(), "3", "有")
                    .when(apiApplyParameter.getCarInfo(), "4", "有")
                    .end("无");
            data.setCar(value);
        }
        // 年龄(如：39)
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        data.setAge(apiFieldConverter.getAgeInt());
        // 工作(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getCareer(), "1", "有")
                    .when(apiApplyParameter.getCareer(), "2", "无")
                    .when(apiApplyParameter.getCareer(), "3", "有")
                    .when(apiApplyParameter.getCareer(), "4", "有")
                    .when(apiApplyParameter.getCareer(), "5", "有")
                    .end("无");
            data.setJob(value);
        }
        // 房(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                    .when(apiApplyParameter.getRealEstateInfo(), "2", "有")
                    .when(apiApplyParameter.getRealEstateInfo(), "3", "有")
                    .when(apiApplyParameter.getRealEstateInfo(), "4", "有")
                    .when(apiApplyParameter.getRealEstateInfo(), "5", "有")
                    .when(apiApplyParameter.getRealEstateInfo(), "6", "有")
                    .end("无");
            data.setHouse(value);
        }
        // 保单(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                    .when(apiApplyParameter.getInsurancePolicy(), "2", "有")
                    .when(apiApplyParameter.getInsurancePolicy(), "3", "有")
                    .when(apiApplyParameter.getInsurancePolicy(), "4", "有")
                    .end("无");
            data.setBaodanIs(value);
        }
        // 性别(“男”或“女”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getSex())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getSex(), "0", "男")
                    .when(apiApplyParameter.getSex(), "1", "女")
                    .end("男");
            data.setSex(value);
        }
        // 贷款额度(如：20，即单位为万)
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            Integer value = ChooseEq.create()
                    .when(apiApplyParameter.getLoanAmount(), "1", 30000)
                    .when(apiApplyParameter.getLoanAmount(), "2", 50000)
                    .when(apiApplyParameter.getLoanAmount(), "3", 100000)
                    .when(apiApplyParameter.getLoanAmount(), "4", 200000)
                    .end(30000);
            data.setMoney(value);
        }
        // 贷款来源
        data.setSource(SOURCE);
        // 贷款来源特别说明
        data.setSourceRemark("实时申请：腾讯百度朋友圈小红书");
        // 社保(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", "无")
                    .when(apiApplyParameter.getSocialSecurity(), "2", "有")
                    .when(apiApplyParameter.getSocialSecurity(), "3", "有")
                    .when(apiApplyParameter.getSocialSecurity(), "4", "有")
                    .end("无");
            data.setShebao(value);
        }
        // 公积金(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "无")
                    .when(apiApplyParameter.getProvidentFund(), "2", "有")
                    .when(apiApplyParameter.getProvidentFund(), "3", "有")
                    .when(apiApplyParameter.getProvidentFund(), "4", "有")
                    .end("无");
            data.setGongjijin(value);
        }
        // 信用卡(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getCreditLimit())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getCreditLimit(), "2", "有")
                    .when(apiApplyParameter.getCreditLimit(), "3", "有")
                    .when(apiApplyParameter.getCreditLimit(), "4", "有")
                    .when(apiApplyParameter.getCreditLimit(), "5", "有")
                    .when(apiApplyParameter.getCreditLimit(), "1", "无")
                    .when(apiApplyParameter.getCreditLimit(), "6", "有")
                    .end("无");
            data.setCreditCard(value);
        }
        //是否有企业(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getBizLicenseYear())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getBizLicenseYear(), "1", "无")
                    .when(apiApplyParameter.getBizLicenseYear(), "2", "有")
                    .when(apiApplyParameter.getBizLicenseYear(), "3", "有")
                    .when(apiApplyParameter.getBizLicenseYear(), "4", "有")
                    .when(apiApplyParameter.getBizLicenseYear(), "5", "有")
                    .end("无");
            data.setCompany(value);
        }
        // 芝麻分(整数,如:567)
        data.setZhima(apiFieldConverter.getSesameCreditText());
        // 银行代发(“银行代发”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发")
                    .when(apiApplyParameter.getSalaryPayMethod(), "2", "无")
                    .when(apiApplyParameter.getSalaryPayMethod(), "3", "无")
                    .end("无");
            data.setIsbankpay(value);
        }
        // 该条数据实际申请时间戳（精确到秒）， 如：1467100609）
        data.setTime(System.currentTimeMillis() / 1000);
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
